How to enable KVM virsh console access for Ubuntu Linux VM

Author: Vivek Gite Last updated: February 19, 2017 2 comments

I just followed your tutorial and setup KVM with Ubuntu Linux as a new guest virtual machine. However, I am unable to use the command: ‘virsh console vm’. How do I setup and enable console access in order to gain shell access on the newly created Ubuntu Linux 16.04 LTS VM?

Activating the serial console in the Ubuntu Linux 16.04 LTS guest is an excellent way to log in and solve problems.

Our sample setup

  1. 1.KVM host IP: 192.168.2.15  

  2. 2.KVM guest OS: Ubuntu Linux 16.04 LTS  

  3. 3.KVM guest IP: 192.168.2.45  

  4. 4.KVM guest name: ubuntu-box1  

  5. 5.KVM guest user name: vivek  

  6. 6.KVM guest password: hidden-secrete  

How do I setup console access of Ubuntu 16.04 KVM Guest

First, you need to login using ssh or VNC client to your Ubuntu guest.

Use ssh login

In this example, I am login using ssh client from my workstation (or type command on KVM host itself) to the Ubuntu Linux VM guest:
$ ssh vivek@192.168.2.45
After log in gain access to the root shell:
$ sudo -s

Use vnc login

Type the following command on your KVM host:
$ virsh dumpxml ubuntu-box1 | grep vnc
<graphics type='vnc' port='5901' autoport='yes' listen='127.0.0.1'>
Please note down the port value (i.e. 5901). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Type the following SSH port forwarding command from your Linux/Unix/MacOS client or desktop system:
$ ssh {user}@{KVM-host-IP-here} -L 5901:127.0.0.1:5901
$ ssh vivek@192.168.2.15 -L 5901:127.0.0.1:5901

Once you have ssh tunnel established, you can point your VNC client at your own IP 127.0.0.1 (localhost) address and port 5901 as follows in VNC client:

Fig.01: Graphical VNC client to connect to Ubuntu Linux 16.04 LTS server consoleFig.01: Graphical VNC client to connect to Ubuntu Linux 16.04 LTS server console

Fig.01: Graphical VNC client to connect to Ubuntu Linux 16.04 LTS server console

Log in to your VM:

Fig.02: Login to my Ubuntu guest VMFig.02: Login to my Ubuntu guest VMFig.02: Login to my Ubuntu guest VMFig.02: Login to my Ubuntu guest VM
 

Fig.02: Login to my Ubuntu guest VM

Configure a serial console in the Ubuntu guest

Finally type the following two commands to enable a serial console in the guest to access a connection using ‘virsh console’ command:
$ sudo systemctl enable serial-getty@ttyS0.service
$ sudo systemctl start serial-getty@ttyS0.service

Sample outputs:

Created symlink from /etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service to /lib/systemd/system/serial-getty@.service.

How do I access console of Ubuntu 16.04 KVM Guest from KVM host

To see a list of running VM, enter:
$ virsh list
Sample outputs:

 Id    Name                           State

----------------------------------------------------

 1     freebsd                        running

 2     ubuntu-box1                    running

 3     ubuntu-box2                    running

 

Type the following command from KVM host to login to the guest named ubuntu-box1
$ virsh console ubuntu-box1
OR
$ virsh console 2
Sample session:

Animated gif 01: virsh console demoAnimated gif 01: virsh console demoAnimated gif 01: virsh console demoAnimated gif 01: virsh console demo
 

Animated gif 01: virsh console demo

Use Ctrl + ]to exit the console.